<?php

class Lien_he extends Admin_Controller {

    public function __construct() {
        parent::__construct();
        $this->load->model("Quanly_m");
        $this->_data['sidebar'] = "lien_he";

        //$this->load->library('formdate');
    }

    public function index() {
        $this->them_moi();
    }

    //-- Them Khach hang --//
    public function them_moi() {
        is_admin();
        $this->_data['page_title'] = "them liên hệ";

        $sale_id = $this->session->userdata('admin_id');
        $perpage = $this->uri->segment(3);
        $offset = $this->uri->segment(4);
        if ($perpage == "") {
            $perpage = 10; //10 records per page
        }
        if ($offset == "") {
            $offset = "";
        }
        $config['base_url'] = base_url() . "index.php/lien_he/them_moi/" . $perpage . "/";
        $config['per_page'] = $perpage;
        $config['uri_segment'] = 4;

        if ($this->session->userdata('admin_role') == '1') {
            $config['total_rows'] = $this->Quanly_m->_get_contacts(0, 0, TRUE);
        } else {
            $config['total_rows'] = $this->Quanly_m->_get_contact_by_sale_id($sale_id, 0, 0, TRUE);
        }

        if ($offset > 0 && $offset == $config['total_rows']) {
            $offset = $offset - $perpage;
        }
        $this->pagination->initialize($config);
        $this->_data['pagination'] = $this->pagination->create_links();
        $this->_data['total'] = $config['total_rows'];
        $this->_data['perpage'] = $perpage;
        $this->_data['offset'] = $offset;

        if ($this->session->userdata('admin_role') == '1') {
            $query = $this->Quanly_m->_get_contacts($perpage, $offset, FALSE);
        } else {
            $query = $this->Quanly_m->_get_contact_by_sale_id($sale_id, $perpage, $offset, FALSE);
        }

        $this->_data['khachhang_data'] = $query;


        // validate form config
        $config = array(
            array("field" => "email",
                "label" => "Email",
                "rules" => "required|trim|xss_clean|valid_email"
            ),
            array("field" => "ten_cong_ty",
                "label" => "Tên công ty",
                "rules" => "required|trim|xss_clean"
            ),
            array("field" => "dien_thoai",
                "label" => "Số điện thoại",
                "rules" => "required|trim|xss_clean"
            )
        );
        $this->form_validation->set_rules($config); //set validation rules

        if ($this->input->post('submit')) {
            if ($this->form_validation->run() === TRUE) {

                if ($this->Quanly_m->_check_exist_contact($this->input->post('dien_thoai')) == 0) {
                    $this->session->set_flashdata('warning', 'Khách hàng' . $this->input->post('ho_ten') . 'đã tồn tại trong cơ sở dữ liệu');
                    redirect('lien_he');
                } else {
                    $sale_id = $this->session->userdata('admin_id');
                    $data_khachhang = array(
                        'id_sale' => $sale_id,
                        'ten_cong_ty' => $this->input->post('ten_cong_ty'),
                        'email' => $this->input->post('email'),
                        'dien_thoai' => $this->input->post('dien_thoai')
                    );

                    $query = $this->Quanly_m->insert_data('lienhe', $data_khachhang);

                    if ($query === TRUE) {
                        $this->session->set_flashdata('error', 'Thêm mới thành công');
                        redirect('lien_he/them_moi');
                    } else {
                        $this->session->set_flashdata('error', 'Thêm mới thất bại');
                        redirect('lien_he/them_moi');
                    }
                }
            } else {
                $this->display_admin('admin/quanly/them_lien_he');
            }
        } else {

            $this->display_admin('admin/quanly/them_lien_he');
        }
    }

    //-- List lien he --//
    public function danh_sach() {
        is_admin();
        $this->_data['page_title'] = "danh sach lien he";
        $sale_id = $this->session->userdata('admin_id');
        $perpage = $this->uri->segment(3);
        $offset = $this->uri->segment(4);
        if ($perpage == "") {
            $perpage = 10; //10 records per page
        }
        if ($offset == "") {
            $offset = "";
        }
        $config['base_url'] = base_url() . "index.php/lien_he/danh_sach/" . $perpage . "/";
        $config['per_page'] = $perpage;
        $config['uri_segment'] = 4;
        if ($this->session->userdata('admin_role') == '1') {
            $config['total_rows'] = $this->Quanly_m->_get_contacts(0, 0, TRUE);
        } else {
            $config['total_rows'] = $this->Quanly_m->_get_contact_by_sale_id($sale_id, 0, 0, TRUE);
        }

        if ($offset > 0 && $offset == $config['total_rows']) {
            $offset = $offset - $perpage;
        }
        $this->pagination->initialize($config);
        $this->_data['pagination'] = $this->pagination->create_links();
        $this->_data['total'] = $config['total_rows'];
        $this->_data['perpage'] = $perpage;
        $this->_data['offset'] = $offset;

        if ($this->session->userdata('admin_role') == '1') {
            $query = $this->Quanly_m->_get_contacts($perpage, $offset, FALSE);
        } else {
            $query = $this->Quanly_m->_get_contact_by_sale_id($sale_id, $perpage, $offset, FALSE);
        }

        $this->_data['khachhang_data'] = $query;
        $this->display_admin("admin/quanly/danh_sach_lien_he");
    }

    //-- Sua thong tin khach hang --//
    public function sua_thong_tin($id) {
        is_admin();
        $this->_data['page_title'] = "Sửa thông tin liên hệ";

        $sale_id = $this->session->userdata('admin_id');
        $perpage = $this->uri->segment(4);
        $offset = $this->uri->segment(5);
        if ($perpage == "") {
            $perpage = 10; //10 records per page
        }
        if ($offset == "") {
            $offset = "";
        }
        $config['base_url'] = base_url() . "index.php/lien_he/sua_thong_tin/" . $id . '/' . $perpage . "/";
        $config['per_page'] = $perpage;
        $config['uri_segment'] = 5;
        if ($this->session->userdata('admin_role') == '1') {
            $config['total_rows'] = $this->Quanly_m->_get_contacts(0, 0, TRUE);
        } else {
            $config['total_rows'] = $this->Quanly_m->_get_contact_by_sale_id($sale_id, 0, 0, TRUE);
        }
        if ($offset > 0 && $offset == $config['total_rows']) {
            $offset = $offset - $perpage;
        }
        $this->pagination->initialize($config);
        $this->_data['pagination'] = $this->pagination->create_links();
        $this->_data['total'] = $config['total_rows'];
        $this->_data['perpage'] = $perpage;
        $this->_data['offset'] = $offset;

        if ($this->session->userdata('admin_role') == '1') {
            $query = $this->Quanly_m->_get_contacts($perpage, $offset, FALSE);
        } else {
            $query = $this->Quanly_m->_get_contact_by_sale_id($sale_id, $perpage, $offset, FALSE);
        }
        $this->_data['khachhang_data'] = $query;

        $khachhang_data = $this->Quanly_m->_get_data_by_id("lienhe", $id);
        $this->_data['data'] = $khachhang_data;
        if ($this->input->post('submit')) {
            $config = array(
                array("field" => "email",
                    "label" => "Email",
                    "rules" => "required|trim|xss_clean|valid_email"
                ),
                array("field" => "ten_cong_ty",
                    "label" => "Tên công ty",
                    "rules" => "required|trim|xss_clean"
                ),
                array("field" => "dien_thoai",
                    "label" => "Số điện thoại",
                    "rules" => "required|trim|xss_clean"
                )
            );
            $this->form_validation->set_rules($config); //set validation rules

            if ($this->form_validation->run() === TRUE) {
                if ($this->Quanly_m->_check_exist_contact_edit($id, $this->input->post('dien_thoai')) == 0) {
                    $this->session->set_flashdata('warning', 'Khách hàng' . $this->input->post('ho_ten') . 'đã tồn tại trong cơ sở dữ liệu');
                    redirect('lien_he/sua_thong_tin/' . $id);
                } else {
                    $data_khachhang = array(
                        'ten_cong_ty' => $this->input->post('ten_cong_ty'),
                        'email' => $this->input->post('email'),
                        'dien_thoai' => $this->input->post('dien_thoai')
                    );

                    $query = $this->Quanly_m->update_data('lienhe', $data_khachhang, $id);
                    if ($query === TRUE) {
                        $this->session->set_flashdata('error', 'Cập nhật thành công');
                        redirect('lien_he');
                    } else {
                        $this->session->set_flashdata('error', 'Cập nhật thất bại');
                        redirect('lien_he');
                    }
                }
            }
        } else {
            $this->display_admin('admin/quanly/sua_lien_he');
        }
    }

    //-- Xoa thong tin khach hang --//
    public function xoa_thong_tin($id) {
        if ($this->Quanly_m->delete_data('lienhe', $id) === TRUE) {
            $this->session->set_flashdata('error', 'Xóa thành công');
            redirect('lien_he');
        } else {
            $this->session->set_flashdata('error', 'Xóa thất bại');
            redirect('lien_he');
        }
    }

}